package de.hawhamburg.androidwecker;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import de.hawhamburg.androidwecker.dummy.DummyContent;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Switch;
import android.widget.TextView;

public class LivingRoomDetailFragment extends Fragment {

    public static final String ARG_ITEM_ID = "item_id";
    public static final int SEEK_BAR_MAX = 100;
    public static final int SEEK_BAR_MIN = 0;
    
    //Array of RGB intensity
    private static int[] loungeIntensity = new int[3];
    
	View rootView = null;

    DummyContent.DummyItem mItem;

    public LivingRoomDetailFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments().containsKey(ARG_ITEM_ID)) {
            mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
        }
        //jsonDemo();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        

        
        if (mItem.content == "Licht") {//Light
        	rootView = inflater.inflate(R.layout.fragment_livingroom_detail_light, container, false);
            ((TextView) rootView.findViewById(R.id.livingroom_detail)).setText(mItem.content);
            lightListeners();
        } else if (mItem.content == "Fenster") {
        	rootView = inflater.inflate(R.layout.fragment_livingroom_detail_windows, container, false);
            ((TextView) rootView.findViewById(R.id.livingroom_detail)).setText(mItem.content);
            windowListeners();
        } else {
        	rootView = inflater.inflate(R.layout.fragment_livingroom_detail, container, false);
        }
        return rootView;
    }
    
    
    /*
     * Listeners for Window Control
     * 
     * 
     */
    
    private void windowListeners() {
    	
		final Switch window1 = (Switch) rootView.findViewById(R.id.switch_window1);
		
		window1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			public void onCheckedChanged(CompoundButton arg0,
					boolean on) {
				
				}
				
		    	CommunicationHelper com = new CommunicationHelper();
				//com.setLightColor(loungeIntensity, "lounge_light_color");
				
			
			
		});
		
		final Switch window2 = (Switch) rootView.findViewById(R.id.switch_window2);
		
		window2.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			public void onCheckedChanged(CompoundButton arg0,
					boolean on) {
				
				}
				
		    	CommunicationHelper com = new CommunicationHelper();
				//com.setLightColor(loungeIntensity, "lounge_light_color");
				
			
			
		});
		
		final Switch window3 = (Switch) rootView.findViewById(R.id.switch_window3);
		
		window3.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			public void onCheckedChanged(CompoundButton arg0,
					boolean on) {
				
				}
				
		    	CommunicationHelper com = new CommunicationHelper();
				//com.setLightColor(loungeIntensity, "lounge_light_color");
				
			
			
		});
		
		final Switch window4 = (Switch) rootView.findViewById(R.id.switch_window4);
		
		window4.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			public void onCheckedChanged(CompoundButton arg0,
					boolean on) {
				
				}
				
		    	CommunicationHelper com = new CommunicationHelper();
				//com.setLightColor(loungeIntensity, "lounge_light_color");
				
			
			
		});
		
		final Switch window5 = (Switch) rootView.findViewById(R.id.switch_window5);
		
		window5.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			public void onCheckedChanged(CompoundButton arg0,
					boolean on) {
				
				}
				
		    	CommunicationHelper com = new CommunicationHelper();
				//com.setLightColor(loungeIntensity, "lounge_light_color");
				
			
			
		});
		
		final Switch windowAll = (Switch) rootView.findViewById(R.id.loungeWindowSwitch);
		
		windowAll.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			public void onCheckedChanged(CompoundButton arg0,
					boolean on) {
					
				System.out.println(on);
					window1.setChecked(on);
					window2.setChecked(on);
					window3.setChecked(on);
					window4.setChecked(on);
					window5.setChecked(on);
				
				}
				
		    	CommunicationHelper com = new CommunicationHelper();
				//com.setLightColor(loungeIntensity, "lounge_light_color");
				
			
			
		});
    	
    }
    
    
    
    /*
     * Listeners for Light Control
     * 
     * 
     */
    
    private void lightListeners() {
    	
	final SeekBar seekBarBlue = (SeekBar) rootView.findViewById(R.id.seekBarLoungeBlue);
			
		seekBarBlue.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
		    public void onProgressChanged(SeekBar seekbar, int progress,
		    	      boolean fromUser) {
		    	
		    	loungeIntensity[2]=progress;

		    	
		    }

			public void onStartTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
				
			}

			public void onStopTrackingTouch(SeekBar arg0) {
		    	CommunicationHelper com = new CommunicationHelper();
				com.setLightColor(loungeIntensity, "lounge_light_color");
				
			}
		});
		
		final SeekBar seekBarRed = (SeekBar) rootView.findViewById(R.id.seekBarLoungeRed);
				
			seekBarRed.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
				
			    public void onProgressChanged(SeekBar seekbar, int progress,
			    	      boolean fromUser) {
			    	
			    	loungeIntensity[0]=progress;

			    	
			    }

				public void onStartTrackingTouch(SeekBar arg0) {
					// TODO Auto-generated method stub
					
				}

				public void onStopTrackingTouch(SeekBar arg0) {
			    	CommunicationHelper com = new CommunicationHelper();
					com.setLightColor(loungeIntensity, "lounge_light_color");
					
				}
			});
			
			final SeekBar seekBarGreen = (SeekBar) rootView.findViewById(R.id.seekBarLoungeGreen);

				seekBarGreen.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
					
				    public void onProgressChanged(SeekBar seekbar, int progress,
				    	      boolean fromUser) {
				    	
				    	loungeIntensity[1]=progress;

				    	
				    }

					public void onStartTrackingTouch(SeekBar arg0) {
						// TODO Auto-generated method stub
						
					}

					public void onStopTrackingTouch(SeekBar arg0) {
				    	CommunicationHelper com = new CommunicationHelper();
						com.setLightColor(loungeIntensity, "lounge_light_color");
						
					}
				});
				Switch loungeLight = (Switch) rootView.findViewById(R.id.loungeLightSwitch);
				
				loungeLight.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					public void onCheckedChanged(CompoundButton arg0,
							boolean on) {
						if(on) {
							//Set Array Values
							loungeIntensity[0]=SEEK_BAR_MAX;
							loungeIntensity[1]=SEEK_BAR_MAX;
							loungeIntensity[2]=SEEK_BAR_MAX;
							
							//Set Progress Bar Values
							seekBarGreen.setProgress(SEEK_BAR_MAX);
							seekBarBlue.setProgress(SEEK_BAR_MAX);
							seekBarRed.setProgress(SEEK_BAR_MAX);
						} else {
							//Set Array Values
							loungeIntensity[0]=SEEK_BAR_MIN;
							loungeIntensity[1]=SEEK_BAR_MIN;
							loungeIntensity[2]=SEEK_BAR_MIN;
							
							//Set Progress Bar Values
							seekBarGreen.setProgress(SEEK_BAR_MIN);
							seekBarBlue.setProgress(SEEK_BAR_MIN);
							seekBarRed.setProgress(SEEK_BAR_MIN);
						}
						
				    	CommunicationHelper com = new CommunicationHelper();
						com.setLightColor(loungeIntensity, "lounge_light_color");
						
					}
					
				});
				
    }
    

    
    
    
//    private void jsondemo() {
//    	map<string,string> valuemap = new hashmap<string, string>();
//    	valuemap.put("red","255");
//    	valuemap.put("green","0");
//    	valuemap.put("blue","128");
//    	valuemap.put("fadetime","0");
//    	map<string,object> aendernmap = new hashmap<string, object>();
//    	aendernmap.put("values", valuemap);
//    	aendernmap.put("id", "client_1578909153");
//    	aendernmap.put("version", null);
//    	aendernmap.put("action", "dining_light_color");
//    	jsonarray jsoa = new jsonarray(arrays.aslist(new integer[]{1,2,3}));
//    	aendernmap.put("anary", jsoa );
//    	jsonobject msgfarbeesszimmeraendern = new jsonobject(aendernmap);
//    	log.d("jsondemo", msgfarbeesszimmeraendern.tostring());
//    	
//    	message msg = new message();
//    	msg.settopic("lp.lightcontrol");
//    	msg.settyp("topic");
//    	msg.setmessage(msgfarbeesszimmeraendern.tostring());
//    	
//    	
//    	communicationhelper com = new communicationhelper();
//    	com.statuschanged(msg);
//        }
}
